package com.cdd.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.commonutils.R;
import com.cdd.eduservice.service.IEduTeacherService;
import com.cdd.eduservice.entity.EduTeacher;
import com.cdd.eduservice.entity.vo.TeacherQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-01-26
 */
@Api(description = "讲师controller")
@RestController //@ResponseBody相应json数据
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    //controller注入service，service里注入mapper
    @Autowired
    private IEduTeacherService iEduTeacherService;

    //查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> eduTeachers = iEduTeacherService.list(null);
        return R.ok().data("items",eduTeachers);
    }

    //逻辑删除指定讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("deleteTeacher/{id}")  //@PathVariable根据路径获取参数
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean flag = iEduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //分页查询讲师
    @ApiOperation(value = "讲师分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                             @ApiParam(name = "limit", value = "页面显示条数", required = true) @PathVariable long limit){

        Page<EduTeacher> page = new Page<>(current, limit);
        iEduTeacherService.page(page, null);
        long total = page.getTotal();   //获取总记录数
        List<EduTeacher> records = page.getRecords();   //数据list

        //两种写法
//        Map map = new HashMap();
//        map.put("total", total);
//        map.put("rows", records);
//        return R.ok().data(map);
        return R.ok().data("total", total).data("rows", records);
    }

    //条件查询带分页的方法
    @ApiOperation("带参数分页查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){  //@RequestBody(required):使用json传递数据，把json数据封装到对应对象里面,
                                                                //required = false表示该参数可以没有，当使用了RequestBody，就需要使用PostMapping注解

        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis 动态sql，有些条件可能有可能没有
        //判断条件是否为空，如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //根据时间排序
        wrapper.orderByDesc("gmt_create");
        //实现查询功能
        iEduTeacherService.page(page, wrapper);
        long total = page.getTotal();   //获取总记录数
        List<EduTeacher> records = page.getRecords();   //数据list

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);

    }

    //添加讲师接口方法
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        //自动时间注入
        Date date = new Date();
        Instant instant = date.toInstant(); //毫秒
        ZoneId zoneId = ZoneId.systemDefault(); //时区

        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        eduTeacher.setGmtModified(localDateTime);
        eduTeacher.setGmtCreate(localDateTime);
        boolean save = iEduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }

    }

    //根据ID查询讲师
    @ApiOperation("根据ID查询讲师")
    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher teacher = iEduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }


    //讲师修改
    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){

        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        //毫秒设置时区，转换为localDateTime
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        eduTeacher.setGmtModified(localDateTime);
        boolean update = iEduTeacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }
    }

}

